package com.atguigu.gmall.pms.service.impl;

import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.entity.SkuAttrValueEntity;
import com.atguigu.gmall.pms.entity.SpuAttrValueEntity;
import com.atguigu.gmall.pms.entity.vo.AttrGroupVo;
import com.atguigu.gmall.pms.mapper.AttrMapper;
import com.atguigu.gmall.pms.mapper.SkuAttrValueMapper;
import com.atguigu.gmall.pms.mapper.SpuAttrValueMapper;
import com.atguigu.gmall.pms.vo.AttrValueVo;
import com.atguigu.gmall.pms.vo.ItemGroupVo;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.AttrGroupMapper;
import com.atguigu.gmall.pms.entity.AttrGroupEntity;
import com.atguigu.gmall.pms.service.AttrGroupService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroupEntity> implements AttrGroupService {

    @Resource
    private AttrMapper attrMapper;
    @Autowired
    private SpuAttrValueMapper baseMapper;  // 基础属性
    @Autowired
    private SkuAttrValueMapper saleMapper; // 销售属性

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<AttrGroupEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageResultVo(page);
    }

    /**
     * 根据category_id查询参数分组表  再根据参数分组表id查询参数表
     *
     * @param cid
     * @return
     */
    @Override
    public List<AttrGroupVo> queryWithAttrsByCid(Long cid) {
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", cid);
        List<AttrGroupEntity> attrGroupEntities = this.list(queryWrapper);
        //判断集合对象是否为null
        if (Collections.isEmpty(attrGroupEntities)) {
            return null;
        }

        ArrayList<AttrGroupVo> attrGroupVos = new ArrayList<>();

        //将attrGroupEntities中的属性写入到attrGroupVo中，还差一个规格参数表属性attrEntities
        for (AttrGroupEntity attrGroupEntity : attrGroupEntities) {
            AttrGroupVo attrGroupVo = new AttrGroupVo();
            BeanUtils.copyProperties(attrGroupEntity, attrGroupVo);
            //根据参数分组表id查询具体参数表
            //注意 只能查询出spu的属性值，type=1 基础属性 需要加上type = 1这个要素
            List<AttrEntity> attrList = attrMapper.selectList(new QueryWrapper<AttrEntity>().eq("group_id", attrGroupEntity.getId()).eq("type", 1));
            //将查询到的规格参数表设置到Vo属性中
            attrGroupVo.setAttrEntities(attrList);
            attrGroupVos.add(attrGroupVo);

        }
//        List<Long> GroupIds = attrGroupEntities.stream().map(AttrGroupEntity::getId).collect(Collectors.toList()); 失败的写法，只取出了分组id
        /*return attrGroupEntities.stream().map(attrGroupEntity -> {
            //
            AttrGroupVo attrGroupVo = new AttrGroupVo();
            BeanUtils.copyProperties(attrGroupEntity, attrGroupVo);
            List<AttrEntity> attrEntities = this.attrMapper.selectList(new QueryWrapper<AttrEntity>().eq("group_id", attrGroupEntity.getId()).eq("type", 1));
            attrGroupVo.setAttrEntities(attrEntities);
            return attrGroupVo;
        }).collect(Collectors.toList());*/


        return attrGroupVos;
    }

    /**
     * 根据三个id查询分组信息及组下的参数信息
     *
     * @param cid
     * @param skuId
     * @param spuId
     * @return
     */
    @Override
    public List<ItemGroupVo> queryGroupWithAttrAndValueByCidAndSpuIdAndSkuId(Long cid, Long skuId, Long spuId) {

        // 根据 三级分类cid 查询分组
        List<AttrGroupEntity> groupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("category_id", cid));
        if (Collections.isEmpty(groupEntities)) {
            return null;
        }

        // 把分组Entity集合转为Vo集合
        return groupEntities.stream().map(attrGroupEntity -> {
            ItemGroupVo itemGroupVo = new ItemGroupVo();
            // 设置参数
            itemGroupVo.setId(attrGroupEntity.getId());
            itemGroupVo.setName(attrGroupEntity.getName());
            // 设置分组参数名称及值
            // 根据分组id查询分组下的规格参数集合
            List<AttrEntity> attrEntities = this.attrMapper.selectList(new QueryWrapper<AttrEntity>().eq("group_id", attrGroupEntity.getId()));
            // 判空
            if (!CollectionUtils.isEmpty(attrEntities)) {
                // 获取规格参数ids 集合
                List<Long> attrIds = attrEntities.stream().map(AttrEntity::getId).collect(Collectors.toList());
                // 分组下的规格参数值集合
                List<AttrValueVo> attrValues = new ArrayList<>();

                // 结合spuId 查询基本类型的规格参数和值
                List<SpuAttrValueEntity> spuAttrValueEntityList = this.baseMapper.selectList(new QueryWrapper<SpuAttrValueEntity>().eq("spu_id", spuId).in("attr_id", attrIds));
                //转为Vo集合  设置到集合中
                if (!CollectionUtils.isEmpty(spuAttrValueEntityList)) {
                    // 集合不为空才进行设置
                    attrValues.addAll(spuAttrValueEntityList.stream().map(spuAttrValueEntity -> {
                        AttrValueVo attrValueVo = new AttrValueVo();
                        // 由于内部元素一致 所以直接copy转换
                        BeanUtils.copyProperties(spuAttrValueEntity, attrValueVo);

                        return attrValueVo;
                    }).collect(Collectors.toList()));
                }
                // 结合skuId 查询销售类型的规格参数和值 与spu基本属性一致
                List<SkuAttrValueEntity> skuAttrValueEntities = this.saleMapper.selectList(new QueryWrapper<SkuAttrValueEntity>().eq("sku_id", skuId).in("attr_id", attrIds));
                if (!CollectionUtils.isEmpty(skuAttrValueEntities)) {
                    attrValues.addAll(skuAttrValueEntities.stream().map(skuAttrValueEntity -> {
                        AttrValueVo attrValueVo = new AttrValueVo();

                        BeanUtils.copyProperties(skuAttrValueEntity, attrValueVo);

                        return attrValueVo;
                    }).collect(Collectors.toList()));
                }
                itemGroupVo.setAttrs(attrValues);
            }
            return itemGroupVo;
        }).collect(Collectors.toList());
    }

}